export const set = (key, val) => {
    return uni.setStorageSync(key, JSON.stringify(val));
    // return localStorage.setItem(key, JSON.stringify(val));
};

export const get = (key) => {
    try {
        let res = uni.getStorageSync(key);
        return JSON.parse(res);
    } catch {
        return null;
    }
    // if (localStorage.getItem(key)) return JSON.parse(localStorage.getItem(key));
    // else return null;
};

export const clear = (key) => {
    if (key) localStorage.removeItem(key);
    else localStorage.clear();
};

// export const sessionSet = (key, val) => {
//   if (key != null && val != null) {
//     return sessionStorage.setItem(key, JSON.stringify(val));
//   }
// };

// export const sessionGet = (key) => {
//   if (sessionStorage.getItem(key))
//     return JSON.parse(sessionStorage.getItem(key));
//   else return null;
// };

// export const sessionClear = (key) => {
//   if (key) sessionStorage.removeItem(key);
//   else sessionStorage.clear();
// };

export const getToken = () => {
    try {
        let res = uni.getStorageSync('auth');
        if (typeof(JSON.parse(res).token) == 'string') {
            return JSON.parse(res).token
        } else {
            return JSON.parse(res).token;
        }
    } catch {
        return null;
    }
};